/*
 * Copyright 2010, 2011 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen.sax;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import atma.jconfiggen.Config;
import atma.jconfiggen.Param;
import atma.jconfiggen.Expr;
import atma.jconfiggen.Seq;

class RootH extends ElemH
{
	private static final AttrMatcher configAttr = new AttrMatcher("config","package","class");

	private ElemH notconfig() throws SAXException {return complain("Root element must be config");}

	RootH(SAXHandler s) {super(s);}

	ElemH elem(SAXHandler.Elem e,Attributes attr) throws SAXException
	{
		if (e != SAXHandler.Elem.CONFIG) notconfig();
		String[] atv = configAttr.match(saxh.loc,attr);
		if (atv[1] == null) missAttr("config","class");
		ArrayList<String> imps = new ArrayList<String>(20);
		ArrayList<Param> params = new ArrayList<Param>(5);
		ArrayList<Expr> sub = new ArrayList<Expr>(10);
		saxh.config = new Config(atv[0],atv[1],imps,params,new Seq(sub));
		return new ConfigH(saxh,imps,params,sub);
	}

	ElemH elemstr(String tag,Attributes attr) throws SAXException
	{
		if (tag.equals("config")) complain("The config tag needs to be in the \"" + SAXHandler.nmsp + "\" namespace");
		return notconfig();
	}
}
